
local Reu=function(giteer,giteestore,path)--
  links="https://gitee.com/"..giteer.."/"..giteestore.."/raw/master/main/"..path
end
function UPDATA_apps(URL,PURL,headerua)
  --If you have any questions, please link to https://gitee.com/luo-ying2020
import "android.content.Context"
import "android.net.ConnectivityManager"
  local function tips(text,color)
    text=tostring(text)
    if color==nil then
      color="#CC4E93D8"
    end
    color=tostring(color)
    Toastbody={
      LinearLayout,
      layout_width="100%w";
      gravity="center";
      layout_height="fill";
      background=color;
      {
        TextView;
        gravity="center";
        text="nothing!";
        id="Toast_TextView";
        textColor="#FFFDFDFD";
        textSize=="15sp";
        layout_width="110%w";
        layout_height="36dp";
      };
    }
    local Toastbody=Toast.makeText(activity,"内容",Toast.LENGTH_SHORT).setView(loadlayout(Toastbody))
    Toastbody.setGravity(Gravity.BOTTOM,0,0)
    Toast_TextView.Text=tostring(text)
    Toastbody.show()
  end
  --此处编写更新后定义防止函数不生效
  --导入包
  import 'android.text.SpannableString'
  import 'android.text.style.ForegroundColorSpan'
  import 'android.text.Spannable'
  if headerua==nil then
    local header = {
      ['User-Agent'] = 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
    }
    else
      local header={
          ['User-Agent'] = headerua
    }
  end
  local function suppress(content)--suppress
    content=tostring(content)
    --处理获取的内容
    content = content:gsub('<br>', '') or content
    content = content:gsub('<div>', '') or content
    content = content:gsub('</div>', '') or content
    content = content:gsub('amp;', '') or content
    content = content:gsub('&nbsp;', '') or content
    content = content:gsub('<br>', '\n') or content
    --   content=content:gsub("<","") or content;
    --    content=content:gsub("/","") or content;
    --    content=content:gsub(">","") or content;
    content = content:gsub('<p>', '\n') or content
    content = content:gsub('</p>', '\n') or content
    return content
  end
  local function up_main(content)
    packinfo =
    this.getPackageManager().getPackageInfo(
    this.getPackageName(),
    ((32552732 / 2 / 2 - 8183) / 10000 - 6 - 231) / 9
    )
    oldve = tostring(packinfo.versionName) --版本信息
    --oldve=tonumber(activity.getPackageManager().getPackageInfo(包名, 0).versionName)
    newve =tostring(content:match("【upto】(.-)【upto】"))
    upintro =tostring(content:match("【intro】(.-)【intro】"))
    applink=tostring(content:match("【link】(.-)【link】"))
    forceor=tostring(content:match("【force】(.-)【force】"))
    if content:match("【intro】(.-)【intro】")==nil or content:match("【intro】(.-)【intro】")=="" then upintro="错误!更新页面信息缺失!" end
    if content:match("【upto】(.-)【upto】")==nil or content:match("【upto】(.-)【upto】")=="" then upintro="错误!更新页面信息缺失!" end
    return packinfo,oldve,newve,upintro,applink,forceor
  end
  local function showupdata(newve,oldve,upintro,applink,forceor)
    if newve > oldve then
      dis=AlertDialog.Builder(this)
      dis.setTitle("版本更新  "..newve)
      dis.setMessage(upintro)
      dis.setPositiveButton("前往更新",{onClick=function(v)
          if pcall(function()
              import "android.content.Intent"
              import "android.net.Uri"
              url=applink
              viewIntent = Intent("android.intent.action.VIEW",Uri.parse(url))
              activity.startActivity(viewIntent)
            end) then
           else
            tips(("执行失败...."),"#B2EE567D")
          end
          if forceor=="是" then
            os.exit()
          end
      end})
      dis.setNegativeButton("取消",{onClick=function(v)
          if forceor=="是" then
            os.exit()
          end
      end})
      if forceor=="是" then
        dis.setCancelable(false)
      end
      dis.show()
    end--结束对话框
  end
  Http.get(URL,nil,"utf8",header,function(code, content)
    if code==200 then
      content=suppress(content)
      --包名=activity.getPackageName()
      up_main(content)
      showupdata(newve,oldve,upintro,applink,forceor)
     else
      tips(("Failed to detect update"..code),"#B2EE567D")
      Http.get(PURL,nil,"utf8",header,function(code, content)
        if code==200 then
          content=suppress(content)
          --包名=activity.getPackageName()
          up_main(content)
          showupdata(newve,oldve,upintro,applink,forceor)
         else
          tips(("Failed to detect alternate update"..code),"#B2EE567D")
        end
      end)
      --备用地址
    end
  end)
end


